/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.controler;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import samoloty.model.IDataModel;
import samoloty.model.models.ConfigurationModel;
import samoloty.model.models.PaxWeightsModel;
import samoloty.view.InfoSubPanel;
import samoloty.view.MainWindow;
import samoloty.view.MyButton;
import samoloty.view.PaxPanel;

/**
 *
 * @author frankowp
 */
public class PaxPanelControler implements MyControler {

    private PaxPanel panel;
    private Controler controler;
    private IDataModel dataModel;

    public PaxPanelControler(Controler c) {
        controler = c;
        initPanel();
    }

    public PaxPanelControler(Controler c, IDataModel data) {
        controler = c;
        dataModel = data;
        initPanel();
    }

    public PaxPanel getPanel() {
        return panel;
    }

    public void initPanel() {
        panel = new PaxPanel();
        panel.setMaleFocusListener(new updateTTL1Value());
        panel.setFemaleFocusListener(new updateTTL1Value());
        panel.setChildFocusListener(new updateTTL1Value());
        panel.setInfantFocusListener(new updateTTL1Value());

        panel.setCFocusListener(new updateTTL2Value());
        panel.setYFocusListener(new updateTTL2Value());

        panel.set0AFocusListener(new updateTTL3Value());
        panel.set0BFocusListener(new updateTTL3Value());
        panel.set0CFocusListener(new updateTTL3Value());

        panel.setMaleFocusListener(new updateInfoPanel());
        panel.setFemaleFocusListener(new updateInfoPanel());
        panel.setChildFocusListener(new updateInfoPanel());
        panel.setInfantFocusListener(new updateInfoPanel());

        panel.setCFocusListener(new updateInfoPanel());
        panel.setYFocusListener(new updateInfoPanel());

        panel.set0AFocusListener(new updateInfoPanel());
        panel.set0BFocusListener(new updateInfoPanel());
        panel.set0CFocusListener(new updateInfoPanel());

        panel.setPaxWeightsCombo(new updateInfoPanelCombo());

        /*validatory*/
        panel.setMaleFocusListener(new PaxValidation());
        panel.setFemaleFocusListener(new PaxValidation());
        panel.setChildFocusListener(new PaxValidation());
        panel.setInfantFocusListener(new PaxValidation());

        panel.setCFocusListener(new CYValidation());
        panel.setYFocusListener(new CYValidation());

        panel.set0AFocusListener(new OABCValidation());
        panel.set0BFocusListener(new OABCValidation());
        panel.set0CFocusListener(new OABCValidation());

        initData();

        panel.setPaxTTL_1_Border(Color.white);
        panel.setPaxTTL_2_Border(Color.white);
        panel.setPaxTTL_3_Border(Color.white);
    }

    public void initData() {
        if (dataModel != null) {
            for (int i = 0; i < dataModel.getPaxWeights().size(); i++) {
                panel.addPaxWeightComboBoxItem(dataModel.getPaxWeights().get(i).getDisplay());
            }
        }
    }

    public void setUpWindow() {
        MainWindow window = controler.getView();
        panel.setPlotPanel(controler.getPlotPanel());

        InfoPanelControler infoPanelControler = controler.getInfoPanelControler();
        InfoSubPanel infoSubPanel = infoPanelControler.getPanel();
        panel.addInfoSubPanel(infoSubPanel);

        window.setLeftPanelVisible(true);
        window.setRightPanelVisible(true);
        window.setAllBtnStyle(MyButton.ACTIVE_BUTTON);
        window.setPaxBtnStyle(MyButton.CHOOSE_BUTTON);
        panel.firstFocused();
        controler.getPlotPanelControler().update();
    }

    public void reset() {
        panel.reset();
    }

    void setDataModel(IDataModel dataModel) {
        this.dataModel = dataModel;
        initData();
    }

    void updateMax() {
        try {
            ConfigurationModel config = controler.getDowPanelControler().get_vconfig();
            int maxOA = config.getAcMax() + config.getAyMax();
            int maxOB = config.getBcMax() + config.getByMax();
            int maxOC = config.getCcMax() + config.getCyMax();
            int maxC = config.getC();
            int maxY = config.getY();
            panel.setMaxOALab("MAX " + Integer.toString(maxOA));
            panel.setMaxOBLab("MAX " + Integer.toString(maxOB));
            panel.setMaxOCLab("MAX " + Integer.toString(maxOC));
            panel.setMaxYLab("MAX " + Integer.toString(maxY));
            panel.setMaxCLab("MAX " + Integer.toString(maxC));
        } catch (Exception e) {
        }
    }

    public void firstFocused() {
        panel.firstFocused();
    }

    private class updateTTL1Value implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            int vpaxm = 0;
            int vpaxf = 0;
            int vpaxch = 0;

            if (panel.getMaleText().equals("") == false) {
                vpaxm = Integer.parseInt(panel.getMaleText());
            }
            if (panel.getFemaleText().equals("") == false) {
                vpaxf = Integer.parseInt(panel.getFemaleText());
            }
            if (panel.getChildText().equals("") == false) {
                vpaxch = Integer.parseInt(panel.getChildText());
            }

            int vttlpax1 = vpaxm + vpaxf + vpaxch;

            panel.setTTLPax1Text(Integer.toString(vttlpax1));

        }
    }

    private class updateTTL2Value implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            int vpaxc = 0;
            int vpaxy = 0;

            if (panel.getCText().equals("") == false) {
                vpaxc = Integer.parseInt(panel.getCText());
            }
            if (panel.getYText().equals("") == false) {
                vpaxy = Integer.parseInt(panel.getYText());
            }

            int vttlpax2 = vpaxc + vpaxy;

            panel.setTTLPax2Text(Integer.toString(vttlpax2));
        }
    }

    private class updateTTL3Value implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            int vpax0a = 0;
            int vpax0b = 0;
            int vpax0c = 0;

            if (panel.get0AText().equals("") == false) {
                vpax0a = Integer.parseInt(panel.get0AText());
            }
            if (panel.get0BText().equals("") == false) {
                vpax0b = Integer.parseInt(panel.get0BText());
            }
            if (panel.get0CText().equals("") == false) {
                vpax0c = Integer.parseInt(panel.get0CText());
            }

            int vttlpax3 = vpax0a + vpax0b + vpax0c;

            panel.setTTLPax3Text(Integer.toString(vttlpax3));
        }
    }

    private class updateInfoPanel implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            controler.getInfoPanelControler().update();
        }
    }

    private class updateInfoPanelCombo implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            controler.getInfoPanelControler().update();
        }
    }

    /*gettery do obslugi ekranu preview*/
    public String get_vpaxm() {
        String result = panel.get_vpaxm();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpaxf() {
        String result = panel.get_vpaxf();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpaxch() {
        String result = panel.get_vpaxch();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpaxi() {
        String result = panel.get_vpaxi();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpaxc() {
        String result = panel.get_vpaxc();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpaxy() {
        String result = panel.get_vpaxy();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpax0a() {
        String result = panel.get_vpax0a();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpax0b() {
        String result = panel.get_vpax0b();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vpax0c() {
        String result = panel.get_vpax0c();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vttlpax1() {
        String result = panel.get_vttlpax1();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vttlpax2() {
        String result = panel.get_vttlpax2();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public String get_vttlpax3() {
        String result = panel.get_vttlpax3();
        if (result.equals("")) {
            result = "0";
        }
        return result;
    }

    public PaxWeightsModel get_vpaxweights() {
        if (dataModel != null) {
            String selectedWeight = panel.get_vpaxweights();
            ArrayList<PaxWeightsModel> paxData = dataModel.getPaxWeights();
            for (int i = 0; i < paxData.size(); i++) {
                if (paxData.get(i).getDisplay().equals(selectedWeight)) {
                    return paxData.get(i);
                }
            }
        }
        return null;

    }

    ///////////////////////////////PAX WEIGHTS ACTION LISTENERS FOCUS LOST VALIDATION
    private class PaxValidation implements FocusListener {

        public void focusGained(FocusEvent e) {
            controler.getMainValidator().validate();
        }

        public void focusLost(FocusEvent e) {
            try {
                int vttlpax1 = Integer.parseInt(get_vttlpax1());
                ConfigurationModel config = controler.getDowPanelControler().get_vconfig();
                if (vttlpax1 > (config.getC() + config.getY())) {
                    int suma = config.getC() + config.getY();
                    panel.setValidator_1_Text("MAX PAX = " + Integer.toString(suma));
                    panel.setVisibleValidator_1(true);// incorect input
                    setBordersColor(Color.red);
                    panel.setPaxTTL_1_Border(Color.red);
                } else {
                    panel.setVisibleValidator_1(false);
                    setBordersColor(Color.white);
                    panel.setPaxTTL_1_Border(Color.white);
                }
            } catch (NullPointerException ex) {
            }

        }

        public void setBordersColor(Color color) {

            if (!get_vpaxm().equals("0")) {
                panel.setMaleBorder(color);
            }
            if (!get_vpaxch().equals("0")) {
                panel.setChildBorder(color);
            }
            if (!get_vpaxf().equals("0")) {
                panel.setFemaleBorder(color);
            }
            if (!get_vpaxi().equals("0")) {
                panel.setInfantBorder(color);
            }

        }
    }

    private class CYValidation implements FocusListener {

        public void focusGained(FocusEvent e) {
            controler.getMainValidator().validate();
        }

        public void focusLost(FocusEvent e) {
            ConfigurationModel config = controler.getDowPanelControler().get_vconfig();
            int vpaxc = Integer.parseInt(get_vpaxc());
            int vpaxy = Integer.parseInt(get_vpaxy());
            boolean visiblec = false;
            boolean visibley = false;
            if (vpaxc > config.getC()) {
                panel.setValidator_2_Text("MAX C = " + config.getC());
                visiblec = true;
                panel.setCBorder(Color.red);
            } else {
                visiblec = false;
                panel.setCBorder(Color.white);
            }

            if (vpaxy > config.getY()) {
                panel.setValidator_2_Text("MAX Y = " + config.getY());
                panel.setVisibleValidator_2(true);
                visibley = true;
                panel.setYBorder(Color.red);
            } else {
                visibley = false;
                panel.setYBorder(Color.white);
            }

            if (visiblec || visibley) {
                panel.setVisibleValidator_2(true);// incorect input
                panel.setPaxTTL_2_Border(Color.red);
            } else if (!visiblec && !visibley) {
                panel.setVisibleValidator_2(false);
                panel.setPaxTTL_2_Border(Color.white);
            }
            controler.getInfoPanelControler().update();
            controler.getPlotPanel().update();

        }

        public void setBordersColor(Color color) {
        }
    }

    private class OABCValidation implements FocusListener {

        public void focusGained(FocusEvent e) {
            controler.getMainValidator().validate();
        }

        public void focusLost(FocusEvent e) {
            ConfigurationModel config = controler.getDowPanelControler().get_vconfig();
            int vpax0a = Integer.parseInt(get_vpax0a());
            int vpax0b = Integer.parseInt(get_vpax0b());
            int vpax0c = Integer.parseInt(get_vpax0c());
            boolean visiblea = false;
            boolean visibleb = false;
            boolean visiblec = false;
            if (vpax0a > config.getAcMax() + config.getAyMax()) {
                panel.setValidator_3_Text("MAX 0A = " + (config.getAcMax() + config.getAyMax()));
                visiblea = true;
                panel.setOABorder(Color.red);
            } else {
                visiblea = false;
                panel.setOABorder(Color.white);
            }

            if (vpax0b > config.getBcMax() + config.getByMax()) {
                panel.setValidator_3_Text("MAX 0B = " + (config.getBcMax() + config.getByMax()));
                visibleb = true;
                panel.setOBBorder(Color.red);
            } else {
                visibleb = false;
                panel.setOBBorder(Color.white);
            }

            if (vpax0c > config.getCcMax() + config.getCyMax()) {
                panel.setValidator_3_Text("MAX 0C = " + (config.getCcMax() + config.getCyMax()));
                visiblec = true;
                panel.setOCBorder(Color.red);
            } else {
                visiblec = false;
                panel.setOCBorder(Color.white);
            }
            if (visiblea || visibleb || visiblec) {
                panel.setVisibleValidator_3(true);// incorect input
                panel.setPaxTTL_3_Border(Color.red);
            } else if (!visiblea && !visibleb && !visiblec) {
                panel.setVisibleValidator_3(false);
                panel.setPaxTTL_3_Border(Color.white);
            }
            controler.getInfoPanelControler().update();
            controler.getPlotPanel().update();

        }
    }

    public boolean isValid() {
        return panel.isValid();
    }
}
